/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cq.bd.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.cq.base.domain.Page;
import com.cq.bd.dao.SFunctionDao;
import com.cq.bd.dao.SFunctionResoDao;
import com.cq.bd.dao.SMenuDao;
import com.cq.bd.dao.SRoleFunctionDao;
import com.cq.bd.dao.SWorkerFunctionDao;
import com.cq.bd.domain.SFunction;
import com.cq.bd.domain.SFunctionReso;
import com.cq.bd.domain.SMenu;
import com.cq.bd.domain.SRoleFunction;
import com.cq.bd.domain.SWorkerFunction;
import com.cq.bd.service.FuncService;
import com.cq.bd.service.MenuService;

/**
 * 菜单管理
 * 
 * @author huangmy
 * @date   2009-10-5
 */
public class MenuServiceImpl implements MenuService {
    private SMenuDao sMenuDao;
    private FuncService funcService;
    private SFunctionResoDao sFunctionResoDao;
    private SFunctionDao sFunctionDao;
    private SRoleFunctionDao sRoleFunctionDao;
    private SWorkerFunctionDao sWorkerFunctionDao;
    
    @SuppressWarnings("unchecked")
    public void doDeleteMenu(Long menuId) {
        SMenu m = this.sMenuDao.get(menuId);
        if (m != null) {
            this.sMenuDao.remove(m);
            
            List<SFunctionReso> resos = this.sFunctionResoDao.find("from SFunctionReso where resoId=? and resoType=1", menuId);
            if (resos != null) {
                for (SFunctionReso reso : resos) {
                    // 删除权限表
                    SFunction func = this.sFunctionDao.get(reso.getFuncId());
                    if (func != null) {
                        this.sFunctionDao.remove(func); 
                    }
                    // 删除角色与权限对应
                    List<SRoleFunction> roleFuncs = this.sRoleFunctionDao.findBy("funcId", reso.getFuncId());
                    if (roleFuncs != null) {
                        for (SRoleFunction roleFunc : roleFuncs) {
                            this.sRoleFunctionDao.remove(roleFunc); 
                        }
                    }
                    // 删除员工与权限对应表
                    List<SWorkerFunction> workerFuncs = this.sWorkerFunctionDao.findBy("funcId", reso.getFuncId());
                    if (workerFuncs != null) {
                        for (SWorkerFunction workerFunc : workerFuncs) {
                            this.sWorkerFunctionDao.remove(workerFunc);
                        }
                    }
                    
                    // 删除权限与资源对应表
                    this.sFunctionResoDao.remove(reso); 
                }
            }
        }        
    }
    
    @SuppressWarnings("unchecked")
    public List<SMenu> doGetAllNotEndMenus() {
        return this.sMenuDao.find("from SMenu where menuIdend=0 and state=1 order by sortId");
    }
    
    public SMenu doGetMenu(Long menuId) {
        return this.sMenuDao.get(menuId);
    }
    
    public Page pageMenus(Map<String, String> filter, Map<String, String> like, Map<String, String> sort, int pageNo,
            int pageSize) {
        return this.sMenuDao.pagedQuery(filter, like, sort, pageNo, pageSize);
    }

    public void doCreateMenu(SMenu menu) {
        menu.setCreateTime(new Date());
        menu.setUpdateTime(new Date());
        menu.setSortId(999);
        menu.setMenuType(1);
        
        Long menuId = (Long)this.sMenuDao.create(menu);
        
        // 权限
        SFunction func = new SFunction();
        func.setFuncDesc(menu.getMenuDesc());
        func.setFuncName(menu.getMenuName());
        func.setFuncResoType(1);
        func.setFuncType(1);
        func.setState(1);
        Long funcId = (Long)this.funcService.doCreateFunc(func);
        
        // 权限与菜单对应关系
        SFunctionReso reso = new SFunctionReso();
        reso.setFuncId(funcId);
        reso.setResoId(menuId);
        reso.setResoType(1);
        
        this.funcService.doCreateFuncReso(reso);
    }

    public void doChangeMenuState(Long menuId, int state) {
        SMenu menu = this.sMenuDao.get(menuId);
        if (menu != null) {
            menu.setState(state);
            
            this.sMenuDao.update(menu);
        }
    }

    @SuppressWarnings("unchecked")
    public List<SMenu> doGetMenus(Long upperMenuId) {
        List<SMenu> menus = null;
        
        menus = this.sMenuDao.find("from SMenu where menuUpId=? and state=1 order by sortId", upperMenuId.intValue());
        
        return menus != null ? menus : new ArrayList<SMenu>();
    }

    public void doUpdateMenu(SMenu menu) {
        this.sMenuDao.update(menu);
    }

    public void setSMenuDao(SMenuDao menuDao) {
        sMenuDao = menuDao;
    }

    public void setFuncService(FuncService funcService) {
        this.funcService = funcService;
    }

    public void setSFunctionResoDao(SFunctionResoDao functionResoDao) {
        sFunctionResoDao = functionResoDao;
    }

    public void setSFunctionDao(SFunctionDao functionDao) {
        sFunctionDao = functionDao;
    }

    public void setSRoleFunctionDao(SRoleFunctionDao roleFunctionDao) {
        sRoleFunctionDao = roleFunctionDao;
    }

    public void setSWorkerFunctionDao(SWorkerFunctionDao workerFunctionDao) {
        sWorkerFunctionDao = workerFunctionDao;
    }

}
